package model;

import java.util.ArrayList;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import utilities.HibernateUtil;
import entity.Customer;

public class CustomerDAO {

    /**
     * Load Customer in Shop.
     * @return List Customer
     * @throws Exception
     */
    @SuppressWarnings({"unchecked", "finally"})
    public static ArrayList<Customer> getList() throws Exception {
        ArrayList<Customer> list = new ArrayList<Customer>();
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            list = (ArrayList<Customer>) session.createCriteria(Customer.class).list();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {

        }
        session.close();
        return list;
    }

    /**
     * Add New Customer
     * @param Customer
     * @throws Exception
     */
    public static void add(Customer customer) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.save(customer);
            transaction.commit();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
        }
    }

    /**
     * Update Customer
     * @param Customer
     * @throws Exception
     */
    public static void update(Customer customer) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.update(customer);
            transaction.commit();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
        }
    }

    /**
     * Delete Customer
     * @param Customer
     * @throws Exception
     */
    public static void delete(Customer customer) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.delete(customer);
            transaction.commit();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
        }
    }

    /**
     * Find Customer by ID
     * @param id
     * @return Customer
     * @throws Exception
     */
    @SuppressWarnings("finally")
    public static Customer find(int id) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        Customer customer = new Customer();
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            customer = (Customer) session.get(Customer.class, id);
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
            return customer;
        }
    }
}
